package com.rykj.qxj.server.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rykj.qxj.server.dao.NoticeMapper;
import com.rykj.qxj.server.model.entity.Notice;
import com.rykj.qxj.server.services.INoticeManageService;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("noticeManageService")
@Log4j
public class NoticeManageServiceImpl implements INoticeManageService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void saveNotice(Notice notice) {
        notice.setContent( StringEscapeUtils.escapeHtml(notice.getContent()));
        noticeMapper.saveNotice(notice);
    }

    @Override
    public void updateNotice(Notice notice) {
        notice.setContent( StringEscapeUtils.escapeHtml(notice.getContent()));
        noticeMapper.updateNotice(notice);
    }

    @Override
    public void updateIsEnable(Integer noticeId, String isEnable) {
        Map<String,Object> paraMap =new HashMap<>();
        paraMap.put("noticeId",noticeId);
        paraMap.put("isEnable",isEnable);
        noticeMapper.updateIsEnable(paraMap);
    }

    @Override
    public void deleteNoticeById(String noticeIds) {
        noticeMapper.deleteNoticeById(noticeIds);
    }

    @Override
    public PageInfo getNoticeList(Integer pageNum, Integer pageSize, String title, String isEnable) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> paraMap = new HashMap<>();
        if("%".equals(title)){
            title ="\\%";
        }
        paraMap.put("title",title);
        paraMap.put("isEnable",isEnable);
        List<Notice> notices=  noticeMapper.getNoticeList(paraMap);
        PageInfo pageInfo = new PageInfo(notices);
        return  pageInfo;
    }

    @Override
    public Notice getNoticeDetails(Integer noticeId) {
        return noticeMapper.getNoticeDetails(noticeId);
    }

    @Override
    public void updateReadNum(Integer readNum, Integer noticeId) {
        Map<String,Object> paraMap =new HashMap<>();
        paraMap.put("noticeId",noticeId);
        paraMap.put("readNum",readNum);
        noticeMapper.updateReadNum(paraMap);
    }

    @Override
    public Integer getNoticeListCount(String title, String isEnable) {
        Map<String,Object> paraMap = new HashMap<>();
        if("%".equals(title)){
            title ="\\%";
        }
        paraMap.put("title",title);
        paraMap.put("isEnable",isEnable);
        return noticeMapper.getNoticeListCount(paraMap);
    }
}
